package com.zzyl.job;

import com.zzyl.entity.Reservation;
import com.zzyl.mapper.ReservationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class MyTask {
    @Autowired
    private ReservationMapper reservationMapper;
    /**
     * 定时任务
     * 1. 按照固定频率执行（每隔 10秒钟|半个小时|。。。执行）
     * 2. 指定一个时间执行（每天凌晨1点）
     */
//    @Scheduled(fixedRate = 10, timeUnit = TimeUnit.SECONDS)
//    @Scheduled(cron = "0 0/30 * * * ?")
    @Scheduled(cron = "0 0,30 8-18,18 * * ?")
    public void task() {
        LocalDateTime now = LocalDateTime.now().minusMinutes(-30);
        List<Reservation> list = reservationMapper.getTimeoutReservation(now);
        if(list.size()==0){
            return;
        }
        for (Reservation reservation : list) {
            if (reservation.getStatus()==0){
                reservation.setStatus(3);
                reservationMapper.update(reservation);
            }
        }
    }
}
